﻿using Modbus.Common;
using ModbusLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
using ModbusLib.Protocols;
using System.Data;
namespace WPFExcelReport
{

    public partial class PLCDM3011
    { 
        public static object PlcWrite(string command, object SlaveId,object StartAddress, object DataLength, object DataSend)
        {
            byte[] data = null;
            try
            {
                switch (command)
                {
                    case "R04": 
                        //1,288,8
                        data = Read04(System.Convert.ToByte(SlaveId),Convert.ToUInt16(StartAddress), Convert.ToUInt16(DataLength));
                        break;
                    case "W03":
                        //1,288,8
                        data = Write03(System.Convert.ToByte(SlaveId), Convert.ToUInt16(StartAddress), Convert.ToUInt16(DataLength));
                        break;
                    case "W10":
                        //1,288,8
                        data = Write10(System.Convert.ToByte(SlaveId), Convert.ToUInt16(StartAddress), Convert.ToUInt16(DataLength),(UInt16[])DataSend);
                        break;
                }
            }
            catch (Exception ex)
            {
                Helpers.LogHelper.error(ex.StackTrace + ex.Message);
            }
            return data;

        }
    }
    public partial class PLCDM3011
    {
        private static int _transactionId = 0; 
        

        public static byte[] Read04(byte SlaveId, ushort StartAddress, ushort DataLength)
        {
            ModbusClient _driver;
            _driver = new ModbusClient(new ModbusRtuCodec()) { Address = SlaveId };
            byte function = ModbusCommand.FuncReadInputRegisters;
            var command = new ModbusCommand(function) { Offset = StartAddress, Count = DataLength, TransId = _transactionId++ };

            byte[] data = (_driver.GetCommand(command)).ToArray();
            /*
            byte[] data = new byte[8];
            //0B 04 01 20 00 08 F1 50 
            01 20=0*16^3+1*16^2+2*16^1+8+16^0=0+256+32+0
            data[0] = 0x01;
            data[1] = 0x04;
            data[2] = 0x01;
            data[3] = 0x20;
            data[4] = 0x00;
            data[5] = 0x08;
            data[6] = 0xF1;
            data[7] = 0xFA;
            //string dataS = rw + start + count + send + Chr(3);
            01 10 00 8C 00 01 02 A8 01 07 5C
             */
            return data;
        }
        /// <summary>
        /// 01 10 00 8C 00 01 02 E8 01 36 9C ==01 10 00 8c 00 01 c0 22 
        /// 01 10 00 00 00 02 04 00 00 00 00 F3 AF==01 10 00 00 00 02 41 C8
        /// 01 03 00 B0 00 01 85 ED ===01 03 02 00 01 79 84
        /// 01 10 00 B0 00 01 02 00 01 7D A0==01 10 00 B0 00 01 00 2E
        /// </summary>
        /// <param name="SlaveId"></param>
        /// <param name="StartAddress"></param>
        /// <param name="DataLength"></param>
        /// <returns></returns>
        public static byte[] Write10(byte SlaveId, ushort StartAddress, ushort DataLength, UInt16[] DataSend)
        {          
            ModbusClient _driver;
            _driver = new ModbusClient(new ModbusRtuCodec()) { Address = SlaveId };
            byte function = ModbusCommand.FuncWriteMultipleRegisters;
            var command = new ModbusCommand(function) { Offset = StartAddress, Count = DataLength, TransId = _transactionId++, Data = new ushort[DataLength] };
            
            for (int i = 0; i < DataLength; i++)
            {  
                command.Data[i] = DataSend[i];
            }
            byte[] data = (_driver.GetCommand(command)).ToArray();
            /*
            byte[] data = new byte[8];
            //0B 04 01 20 00 08 F1 50 
            data[0] = 0x01;
            data[1] = 0x04;
            data[2] = 0x01;
            data[3] = 0x20;
            data[4] = 0x00;
            data[5] = 0x08;
            data[6] = 0xF1;
            data[7] = 0xFA;
            //string dataS = rw + start + count + send + Chr(3);
            01 10 00 8C 00 01 02 A8 01 07 5C
             */
            return data;
        }
        public static byte[] Write03(byte SlaveId, ushort StartAddress, ushort DataLength)
        {
            ModbusClient _driver;
            _driver = new ModbusClient(new ModbusRtuCodec()) { Address = SlaveId };
            byte function = ModbusCommand.FuncReadMultipleRegisters;
            var command = new ModbusCommand(function) { Offset = StartAddress, Count = DataLength, TransId = _transactionId++ };

            byte[] data = (_driver.GetCommand(command)).ToArray();
            /*
            
           01 03 00 B0 00 01 85 ED 
             */
            return data;
        }


        public static byte[] ClosePLC()
        {
            byte[] data = new byte[39];
            //68 1D 1D 68 02 00 6C 32 01 00 00 00 00 00 10 00 00 29 00 00 00 00 00 09 50 5F 50 52 4F 47 52 41 4D AA 16
            return data;
        }
    }

}
